package com.higgins.eventtracking.client.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.higgins.eventtracking.client.dto.AssetDTO;
import com.higgins.eventtracking.client.dto.TransactionDTO;

public class AssetTable extends FlexTable	{
	private List<AssetDTO> assetDTOs = new ArrayList<AssetDTO>();
	
	public AssetTable()	{
		super();
		initialise();
		addHandler();
	}
	public void addHandler()	{
		addClickHandler(new ClickHandler()	{

			@Override
			public void onClick(ClickEvent event) {
				Cell cell = getCellForEvent(event);
				final int row = cell.getRowIndex();
				final int column = cell.getCellIndex();
			}
		});
	}

	private void addRow(int rowCount, String account, 
			Double amount, 
			String accountType, 
			String accountRepayment,
			Double monthlyRepayment, 
			Double monthlyInterest, 
			Date timestamp)	{
		int column = 0;
		addString(rowCount, column++, timestamp!=null?timestamp.toString():"");
		addString(rowCount, column++, account!=null?account:"");
		addString(rowCount, column++, amount!=null?amount.toString():"");
		addString(rowCount, column++, accountType!=null?accountType:"");
		addString(rowCount, column++, accountRepayment!=null?accountRepayment:"");
		addString(rowCount, column++, monthlyRepayment!=null?monthlyRepayment.toString():"");
		addString(rowCount, column++, monthlyInterest!=null? monthlyInterest.toString():"");
	}
	
	public void initialise()	{
		this.setCellPadding(1);
		this.setCellSpacing(0);
		this.setWidth("750px");
		
		getColumnFormatter().setWidth(0, "30%"); 
		getColumnFormatter().setWidth(1, "20%"); 
		getColumnFormatter().setWidth(2, "10%"); 
		getColumnFormatter().setWidth(3, "10%"); 
		getColumnFormatter().setWidth(4, "10%"); 
		getColumnFormatter().setWidth(5, "10%"); 
		getColumnFormatter().setWidth(6, "10%"); 
		
		this.setText(0,0,"Date");
		this.setText(0,1,"Account");
		this.setText(0,2,"Amount");
		this.setText(0,3,"Type");
		this.setText(0,4,"Repay Acc");
		this.setText(0,5,"Repayment");
		this.setText(0,6,"Interest");
		
		RowFormatter rf = this.getRowFormatter();
		rf.setStyleName(0, "FlexTable-HeaderRow");
		this.getCellFormatter().addStyleName(0, 0, "FlexTable-normCell");
		this.getCellFormatter().addStyleName(0, 1, "FlexTable-normCell");
		this.getCellFormatter().addStyleName(0, 2, "FlexTable-normCell");
		this.getCellFormatter().addStyleName(0, 3, "FlexTable-normCell");
		this.getCellFormatter().addStyleName(0, 4, "FlexTable-normCell");
		this.getCellFormatter().addStyleName(0, 5, "FlexTable-normCell");
		this.getCellFormatter().addStyleName(0, 6, "FlexTable-normCell");
	}	

	private void clearTable()	{
		int rows = getRowCount();
		for (int row = 1; row < rows; row++)
			this.removeRow(getRowCount()-1);
	}
	
	public void addRow(AssetDTO assetDTO, int row)	{
		int rowCount = this.getRowCount();
		if (rowCount <= row)
			this.insertRow(rowCount);
		addRow(rowCount, 
				assetDTO.getAccount(), 
				assetDTO.getAmount(), 
				assetDTO.getAccountType(), 
				assetDTO.getAccountRepayment(),
				assetDTO.getMonthlyRepayment(), 
				assetDTO.getMonthlyInterest(), 
				assetDTO.getTimestamp());
	}
	private void addString(int row, int column, String text)	{
		this.setText(row,column,text);
		getCellFormatter().addStyleName(row, column, "FlexTable-normCell");
	}
	private void addDouble(int row, int column, Double number)	{
		this.setText(row, column,convertDoubleToString(number));
 		getCellFormatter().addStyleName(row, column, "FlexTable-normCell");
	}
	public String convertDoubleToString(Double inValue){
		String stringValue = inValue.toString();
		int positionOfDot = stringValue.indexOf(".");
		int lengthOfNumberText = stringValue.length();
		if ((lengthOfNumberText - positionOfDot) > 4)
			lengthOfNumberText = positionOfDot+4;
		if (positionOfDot>0)
			stringValue = stringValue.substring(0, lengthOfNumberText);
		return stringValue;
	}	

	public void update(List<AssetDTO> assetDTOs) {
		this.assetDTOs = assetDTOs;
		clearTable();
		int row = 0;
		for (AssetDTO assetDTO:assetDTOs)	{
			addRow(assetDTO, row++);
		}
	}

}
